package com.demo.java.OD1_50.OD19;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Collections;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 【比赛评分问题(B卷)】问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/145523417
 */
public class OdMain {
    // 定义一个Person类，用来表示每个选手
    static class Person implements Comparable<Person> {
        private int id;  // 选手ID
        private int total;  // 选手总分
        private List<Integer> scores;  // 每个裁判给出的分数

        // 构造方法
        public Person(int id, int total, List<Integer> scores) {
            this.id = id;
            this.total = total;
            this.scores = scores;
        }

        // 计算某个分数出现的次数
        private int checkCount(List<Integer> list, int count) {
            int cou = 0;
            for (int score : list) {
                if (score == count) {
                    cou++;
                }
            }
            return cou;
        }

        // 自定义排序规则：先按总分排序，再按最高分（10分）次数排序
        @Override
        public int compareTo(Person ply) {
            // 比较总分
            if (ply.total < this.total) {
                return -1;
            } else if (ply.total > this.total) {
                return 1;
            } else {
                // 如果总分相同，则比较最高分次数
                List<Integer> scPlyList = ply.scores;
                List<Integer> scores = this.scores;
                for (int i = 10; i > 0; i--) {
                    int ipl = checkCount(scPlyList, i);
                    int ith = checkCount(scores, i);
                    if (ipl < ith) {
                        return -1;
                    }
                }
            }
            return 0; // 如果依然相同，返回0
        }
    }

    public static void main(String[] args) {
        try {
            Scanner s = new Scanner(System.in);
            String[] nums = s.nextLine().split(",");
            // 读取裁判数和选手数
            int jiaolian = Integer.parseInt(nums[0]);
            int xuanshou = Integer.parseInt(nums[1]);

            // 校验输入是否合法
            if (jiaolian > 10 || jiaolian < 3 || xuanshou > 100 || xuanshou < 3) {
                System.out.println(-1);
                return;
            }

            List<String[]> myList = new ArrayList<>();
            for (int i = 0; i < jiaolian; i++) {
                myList.add(s.nextLine().split(","));
            }

            // 收集选手信息
            List<Person> persons = new ArrayList<>();
            for (int i = 0; i < xuanshou; i++) {
                int total = 0;
                List<Integer> scoreList = new ArrayList<>();
                for (int j = 0; j < jiaolian; ++j) {
                    String[] strings = myList.get(j);
                    int score = Integer.parseInt(strings[i]);
                    if (score < 0 || score > 10) {
                        System.out.println(-1);
                        return;
                    }
                    scoreList.add(score);
                    total += score;
                }
                persons.add(new Person(i, total, scoreList));
            }

            // 排序选手
            Collections.sort(persons);

            // 输出前三名选手的ID（注意是从1开始计数）
            for (int i = 0; i < 3; i++) {
                if (i == 2) {
                    System.out.println(persons.get(i).id + 1);
                } else {
                    System.out.print(persons.get(i).id + 1 + ",");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

